﻿using System.Drawing.Imaging;
using System.IO;
using System.Windows.Media.Imaging;

namespace Aliphant.UI.Forms.Helpers
{
    public class PictureHelper
    {
        /// <summary>
        /// 创建Bitmap对象
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static BitmapImage? CreateImage(string path, int? decodeWidth = null, int? decodeHeight = null)
        {
            if (!File.Exists(path))
            {
                return null;
            }

            try
            {
                var bitmap = new BitmapImage();
                bitmap.BeginInit();
                bitmap.UriSource = new Uri(path, UriKind.RelativeOrAbsolute);
                bitmap.CacheOption = BitmapCacheOption.OnLoad; // 确保流关闭后可用

                if (decodeWidth.HasValue)
                {
                    bitmap.DecodePixelWidth = decodeWidth.Value;
                }
                if (decodeHeight.HasValue)
                {
                    bitmap.DecodePixelHeight = decodeHeight.Value;
                }

                bitmap.EndInit();
                bitmap.Freeze(); // 跨线程安全操作
                return bitmap;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 创建Bitmap对象
        /// </summary>
        /// <param name="path"></param>
        /// <param name="dpi"></param>
        /// <returns></returns>
        public static BitmapImage? CreateImage(string path, int dpi)
        {
            return CreateImage(path, dpi, null);
        }

        /// <summary>
        /// 转换图片为BMP格式，保存到壁纸目录下
        /// 生成三种不同分辨率的bmp位图
        /// </summary>
        /// <param name="picture"></param>
        /// <param name="savePath"></param>
        public static void ConvertToBmp(string picture, string saveDir)
        {
            PictureBox pb = new PictureBox()
            {
                Image = Image.FromFile(picture),
            };

            // 获取图片输出路径
            string pN = Path.GetFileNameWithoutExtension(picture);
            string fN = Path.GetFileName(picture);

            string savePath = Path.Combine(saveDir, pN);
            if (Directory.Exists(savePath))
            {
                Directory.Delete(savePath, true);
            }
            Directory.CreateDirectory(savePath);
            string originPic = Path.Combine(savePath, fN);
            string oBmp = Path.Combine(savePath, "origin.bmp");
            string lBmp = Path.Combine(savePath, "low.bmp");
            string pBmp = Path.Combine(savePath, "preview.bmp");

            // 保存原图和原分辨率的位图
            File.Copy(picture, originPic, true);
            pb.Image.Save(oBmp, ImageFormat.Bmp);

            using Bitmap bitmap = (Bitmap)pb.Image;
            Size presize = new Size(960, 960 * bitmap.Height / bitmap.Width);
            Size lowsize = new Size(200, 200 * bitmap.Height / bitmap.Width);


            using Bitmap previewBmp = new Bitmap(bitmap, presize);
            previewBmp.Save(lBmp, ImageFormat.Bmp);     // 中等质量

            using Bitmap lowBmp = new Bitmap(bitmap, lowsize);
            lowBmp.Save(pBmp, ImageFormat.Bmp);     // 预览图

        }
    }
}
